
var DIALOG_TWOTEAM = 0,
	DIALOG_ONEPLAYER = 1;
	
function openEnterPlayersPage(){
	openStarsPage("enter_players_page");
	//$.mobile.changePage( $("#enter_players_page") );
}

function openEpDialog(dialogType){
	
	switch (dialogType) {
	  case DIALOG_TWOTEAM:
		$("#epDialogText").html(isEn() ? "Number of teams must have at least two" : "Количество команд должно быть не меньше двух");
		$("#twoteam_btn_ok").css("display", "block");
		$("#onep_btn_ok").css("display", "none");
		$("#onep_btn_cancel").css("display", "none");
		break;
	  case DIALOG_ONEPLAYER:
		$("#epDialogText").html(isEn() ? "There should be 2 players. Teams with one player will be deleted." : 
				"В команде должно быть 2 игрока. Команды с одним игроком будут удалены.");
		$("#twoteam_btn_ok").css("display", "none");
		$("#onep_btn_ok").css("display", "block");
		$("#onep_btn_cancel").css("display", "block");
		break;
	}
	
	$("#epDialogBlock").css("display", "block");
	$("#epDialog").addClass("active");
}

function closeEpDialog(){
	$("#epDialogBlock").css("display", "none");
	$("#epDialog").removeClass("active");
}

function checkTwoTeam(){
	if(teams.length < 2){
		openEpDialog(DIALOG_TWOTEAM);
	} else {
		openEditWordsPage();		
	}	
}

$(document).ready(function() {
	$("#players_box").empty();
	for (var i = 0; i < playerCount; i++) {
		var playerName = '';//(i < 4 ) ? 'Игрок '+(i+1) : '';
		var color = teamsInfo[Math.floor(i/2+1)].color;
		
		$("#players_box").append('<input type="text" tag="'+i+'" class="enter_player" style="color: '+color+';" value="'+playerName+'" maxlength="20"/></br>');
			//'<div class="radio radio-men radio-active" tag="'+i+'"/><div class="radio radio-women" tag="'+i+'"/></div>'+
			//'<div style="clear: both;"></div>');
	}
	$("#teams_box").empty();
	for (var i = 0; i < playerCount/2; i++) {
		$("#teams_box").append('<div class="team"><div style="background-color: '+teamsInfo[i+1].color+'; ">'+
			'</div><span class="language" en="Team '+(i+1)+'">Команда '+(i+1)+'</span></div>');
	}


	$("#players_btn_next").click(function(){
		delete teams;
		teams = [];	

		$(".enter_player").each(function(){
			var playerName = $(this).val();
			if(playerName == '') return;
			var index = $(this).attr("tag");
			var teamIndex = Math.floor(index/2);
			if(teams[teamIndex] === undefined) 
				teams[teamIndex] = { players: [], index: teamIndex+1, info: teamsInfo[ teamIndex+1 ] };
			teams[teamIndex].players.push(playerName);
		});
		var onePlayerTeam = false;
		for(var i = 0; i < teams.length; i++ ){
			if(teams[i] === undefined){
				teams.splice(i, 1);
				i--;
				continue;
			} 
			if(teams[i].players.length != 2){
				onePlayerTeam = true;
				teams.splice(i, 1);
				i--;
			}
		}

		if(onePlayerTeam){
			openEpDialog(DIALOG_ONEPLAYER);
		} else {
			checkTwoTeam();
		}
		
		/*
		if(!onePlayerTeam || confirm("В команде должно быть 2 игрока. Команды с одним игроком будут удалены.")){
			if(teams.length < 2){
				//alert("Количесво команд должно быть не меньше двух");
				openEpDialog(DIALOG_TWOTEAM);
			} else {
				openEditWordsPage();		
			}		
		}
		*/
	});	
	
	$("#btn_players_back").click(function(){
		openStarsPage("main_page");
	});
	
	$("#twoteam_btn_ok").click(function(){
		closeEpDialog();
	});
	
	$("#onep_btn_ok").click(function(){
		closeEpDialog();
		checkTwoTeam();
	});

	$("#onep_btn_cancel").click(function(){
		closeEpDialog();
	});	
	
	$("#btn_players_shuffle").click(function(){
		var players = [];
		$(".enter_player").each(function(){
			var p = $(this).val();
			if(p != "") players.push(p);
			$(this).val("");
		});
		shuffle(players);
		$(".enter_player").each(function(){
			$(this).val(players[$(this).attr("tag")]);
		});		
	});
});

